package com.dk.controller;

import com.dk.pojo.CmsChanel;
import com.dk.service.CmsChanelServiceI;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("cmsChanelController")
public class CmsChanelController {

    @Autowired
    private CmsChanelServiceI cmsChanelServiceI;

    //跳转导航页面
    @RequestMapping("list")
    public String listGet(){
        return "cms/list";
    }

    //查询导航
    @RequestMapping("getInfoServer")
    @ResponseBody
    public Map<String,Object> getInfoServer(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "2")Integer pageSize){
        PageInfo<CmsChanel> page = cmsChanelServiceI.getInfoServer(pageNum,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("rows", page.getList());
        return map;
    }

    @RequestMapping("moveUp/{channelId}")
    @ResponseBody
    public boolean moveUp(@PathVariable("channelId")Integer channelId){

        //根据id 查询一条数据
        CmsChanel cmsChanel =cmsChanelServiceI.getUpdate(channelId);
        //获取此数据中的Sorted
        Integer sorted1= cmsChanel.getSorted();
        //查询点击Sorted后倒序结果
        CmsChanel cmsChanel1 =  cmsChanelServiceI.getInfos(cmsChanel);
        if(cmsChanel1 != null){
            //获取倒序的Sorted
            Integer sorted2 = cmsChanel1.getSorted();

            //把倒序的Sorted结果放到第一次结果中
            cmsChanel.setSorted(sorted2);
            cmsChanelServiceI.updateInfo(cmsChanel);

            cmsChanel1.setSorted(sorted1);
            cmsChanelServiceI.updateInfo(cmsChanel1);
            return true;
        }
        return false;
    }

    @RequestMapping("moveDown/{channelId}")
    @ResponseBody
    public boolean moveDown(@PathVariable("channelId")Integer channelId){
        CmsChanel cmsChane2 =cmsChanelServiceI.getUpdate(channelId);
        Integer sorted3 = cmsChane2.getSorted();

        CmsChanel cmsChanel3 =  cmsChanelServiceI.getInfors(cmsChane2);
        if(cmsChanel3 != null){
            Integer sorted4 = cmsChanel3.getSorted();

            cmsChane2.setSorted(sorted4);
            cmsChanelServiceI.updateInfo(cmsChane2);

            cmsChanel3.setSorted(sorted3);
            cmsChanelServiceI.updateInfo(cmsChanel3);
            return true;
        }
        return false;
    }
}
